#include "elibraryparser_journal.h"
#include "elibraryparser_article.h"


//ArticleList

QList<ArticleStruct> eLibraryParser_Journal::GetArticleList(unsigned int journal_id)
{
    _journal_id = journal_id;
    _journal_page = 1;

    return GetArticleList();
}


QList<ArticleStruct> eLibraryParser_Journal::GetArticleList()
{
    QUrl journal_url;
    QList<QPair<QString, QString> > query;

    query.push_back(QPair<QString, QString>("add_all", ""));
    query.push_back(QPair<QString, QString>("author", ""));
//    query.push_back(QPair<QString, QString>("id", QString("%1").arg(_journal_id)));
    //query.push_back(QPair<QString, QString>("issueid", QString("%1").arg(_journal_id)));
    query.push_back(QPair<QString, QString>("authorid", QString("%1").arg(_journal_id)));
    query.push_back(QPair<QString, QString>("itembox_name", ""));
    query.push_back(QPair<QString, QString>("order", "1"));
    query.push_back(QPair<QString, QString>("orgsid", ""));
    query.push_back(QPair<QString, QString>("pagenum", QString("%1").arg(_journal_page)));
    query.push_back(QPair<QString, QString>("payitemid", ""));
    query.push_back(QPair<QString, QString>("paysum", ""));
    query.push_back(QPair<QString, QString>("pubname", ""));
    query.push_back(QPair<QString, QString>("pubyear", ""));
    query.push_back(QPair<QString, QString>("rubriccode", ""));
    query.push_back(QPair<QString, QString>("sortorder", "0"));
//http://elibrary.ru/author_items.asp?authorid=252317
    journal_url.setScheme ("http");
    journal_url.setHost("elibrary.ru");
//    journal_url.setPath("title_items.asp");
    journal_url.setPath("author_items.asp");
    //journal_url.setPath("contents.asp");
    //issueid=869429
    journal_url.setQueryDelimiters('=','&');
    journal_url.setQueryItems(query);
//    author_items.asp?authorid=41590

    QString str = journal_url.toString();

    return WebParser::GetArticleList(journal_url);
}

QList<ArticleStruct> eLibraryParser_Journal::ParsePage(QWebPage *pWebPage)
{
    QList<ArticleStruct> ArticleList;

    QWebElementCollection ArticleElements = pWebPage->mainFrame()->documentElement().findAll("table[id=restab] tr td[align=left] a b");
    foreach (QWebElement ArticleElement, ArticleElements)
    {
        eLibraryParser_Article eLP_A;
        QStringList AuthorNames;
        QString Journal;

        QWebElementCollection Elements = ArticleElement.parent().parent().findAll("font");

        switch(Elements.toList().size())
        {
        case 1:
            Journal = Elements[0].toPlainText();
            break;
        case 2:
            AuthorNames = QString(Elements[0].toPlainText()).split(",", QString::SkipEmptyParts);
            for (QStringList::iterator it = AuthorNames.begin(); it != AuthorNames.end(); ++it)
                *it = it->trimmed();

            Journal = Elements[1].toPlainText();
            break;
        }

        int dotIndex = Journal.indexOf(".");

        eLP_A.SetAuthorNames(AuthorNames);
        eLP_A.SetJournalName(Journal.left(dotIndex));
        eLP_A.SetJournalInfo(Journal.mid(dotIndex + 1));
        eLP_A.SetArticleName(ArticleElement.toPlainText());
        eLP_A.SetArticleUrl(QUrl("http://" + pWebPage->mainFrame()->url().host() + "/" + ArticleElement.parent().attribute("href")));

        ArticleList +=
            eLP_A.GetArticleList(eLP_A.GetArticleUrl());
    }

    if(ArticleElements.toList().size())
    {
        ++_journal_page;
        ArticleList += GetArticleList();
    }

    return ArticleList;
}
